package cn.cxg.ymcc.service.impl;

import cn.cxg.ymcc.api.LoginFeignAPI;
import cn.cxg.ymcc.constants.BusinessConstants;
import cn.cxg.ymcc.domain.Login;
import cn.cxg.ymcc.domain.User;
import cn.cxg.ymcc.domain.UserAccount;
import cn.cxg.ymcc.domain.UserBaseInfo;
import cn.cxg.ymcc.dto.CodeDto;
import cn.cxg.ymcc.dto.RegisterDto;
import cn.cxg.ymcc.exception.GlobleException;
import cn.cxg.ymcc.mapper.UserMapper;
import cn.cxg.ymcc.result.JSONResult;
import cn.cxg.ymcc.service.IUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-08-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private LoginFeignAPI loginFeignAPI;
    @Autowired
    private UserAccountServiceImpl userAccountService;
    @Autowired
    private UserBaseInfoServiceImpl userBaseInfoService;

    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public void phoneRegister(RegisterDto registerDto) {
        String phone = registerDto.getMobile();
        String smsCode = registerDto.getSmsCode();
        String password = registerDto.getPassword();
        Integer regChannel = registerDto.getRegChannel();
        //1.校验
        if (StringUtils.isEmpty(phone)
            ||StringUtils.isEmpty(smsCode)
            ||StringUtils.isEmpty(password)
            ||StringUtils.isEmpty(regChannel)){
            throw new GlobleException("参数不能为空");
        }

        //2/校验验证码
        String key = BusinessConstants.PHONE_REGISTER+phone;
        Object obj = redisTemplate.opsForValue().get(key);
        if (obj==null){
            throw new GlobleException("验证码已经过期");
        }
        //验证码是否正确
         CodeDto value =(CodeDto) obj;
        if (!value.getCode().equals(smsCode)){
            throw new GlobleException("验证码错误");
        }

        //3.校验手机号是否已经注册
        Wrapper<User> www = new EntityWrapper<>();
        www.eq("phone",phone);
        User userphone = selectOne(www);
        if (userphone!=null){
            throw new GlobleException("手机号已经被注册");
        }


        //4.注册 t_user,t_user_account,t_user_base_info,t_login_log

        Login login = new Login();
        login.setUsername(phone);
        login.setPassword(password);
        login.setType(1);
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setCredentialsNonExpired(true);
        login.setAccountNonExpired(true);

        JSONResult jsonResult = loginFeignAPI.register(login);
        //转类型
        Object data = jsonResult.getData();
        String jsonString = JSONObject.toJSONString(data);
        Login loginFormDB = JSONObject.parseObject(jsonString, Login.class);
        Long login_id = loginFormDB.getId();


        //把login_id给user
        User user = new User();
        long now = new Date().getTime();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        user.setPhone(phone);
        user.setNickName(phone);
        user.setBitState(0l);
        user.setSecLevel(0);
        user.setLoginId(login_id);
        insert(user);

        //给t_user_account
        UserAccount userAccount = new UserAccount();
        userAccount.setId(user.getId());
        userAccount.setUsableAmount(new BigDecimal(0));
        userAccount.setFrozenAmount(new BigDecimal(0));
        userAccount.setCreateTime(now);
        userAccount.setUpdateTime(now);
        userAccountService.insert(userAccount);

        //t_user_base_info
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setId(user.getId());
        userBaseInfo.setCreateTime(now);
        userBaseInfo.setUpdateTime(now);
        userBaseInfo.setRegChannel(regChannel);
        userBaseInfoService.insert(userBaseInfo);

        //删除验证码
        redisTemplate.delete(key);



    }
}
